package one.stand.controller;

import com.jmp.api.dto.UserAuthListResponse;
import com.jmp.api.dto.UserInfoResponse;
import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import com.jmp.base.dto.CompanyAuditInfoRequest;
import com.jmp.base.dto.CompanyAuditInfoResponse;
import com.jmp.feign.CompanyFeign;
import io.swagger.annotations.ApiOperation;
import one.stand.dto.ConvertUtils;
import one.stand.model.BusinessCard;
import one.stand.model.Menu;
import one.stand.model.ResultModel;
import one.stand.service.BusinessCardBaseService;
import one.stand.service.impl.CompanyBaseServiceImpl;
import one.stand.service.user.MenuService;
import one.stand.util.AssertUtil;
import one.stand.util.RequestContext;
import one.stand.util.RequestContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * @author cjr
 */
@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private CompanyFeign companyFeign;

    @Autowired
    private MenuService menuService;
    @Autowired
    private BusinessCardBaseService businessCardBaseService;

    @ApiOperation(value = "权限列表")
    @PostMapping("/auth/list")
    @SecurityAuthCheck
    public ResultModel<UserAuthListResponse> authList() {
        final RequestContext.User optUser = RequestContextHolder.getRequestContext().getUser();

        List<Menu> openMenu = menuService.getOpenMenu(optUser.getUserId(), MenuService.TYPE_CRM_APP);
        List<String> openMenuCodes = ConvertUtils.list(openMenu, Menu::getPage);
        UserAuthListResponse response = UserAuthListResponse.builder()
                .auths(openMenuCodes)
                .build();
        return ResultModel.success(response);
    }

    @ApiOperation(value = "我的信息")
    @PostMapping("/info")
    public ResultModel<UserInfoResponse> info() {
        final RequestContext.User optUser = RequestContextHolder.getRequestContext().getUser();
        if (Objects.isNull(optUser)) {
            return ResultModel.success(UserInfoResponse.builder()
                    .auditPass(-1)
                    .build());
        }

        boolean hasCard = Objects.nonNull(optUser.getCardId()) && optUser.getCardId() > 0;
        boolean isNewUser = true;
        if (hasCard) {
            BusinessCard businessCard = businessCardBaseService.getCustomer(optUser.getCardId());
            isNewUser = businessCard.getCreateTime().after(CompanyBaseServiceImpl.DATE_NEW_USER);
        }
        Integer auditPass;
        if (isNewUser && hasCard) {
            BusinessCard businessCard = businessCardBaseService.getCustomer(optUser.getCardId());
            ResultModel<CompanyAuditInfoResponse> companyAuditInfoResponseResultModel = companyFeign.auditInfo(CompanyAuditInfoRequest.builder().companyId(businessCard.getCompanyId()).build());
            AssertUtil.isTrue(companyAuditInfoResponseResultModel);
            auditPass = companyAuditInfoResponseResultModel.getData().getStatus();
        } else if (!isNewUser) {
            auditPass = 1;
        } else {
            auditPass = 2;
        }
        String auditPassMsg = null;
        if (auditPass == 0) {
            auditPassMsg = "您的身份认证已提交，正在审核中，审核结果会以短信的形式通知您，通过后请重新登陆APP即可解锁全部操作！";
        } else if (auditPass == 2) {
            auditPassMsg = "请先创建企业，核实身份才可以进行下一步操作（个人用户请填写姓名上传身份证）";
        }
        UserInfoResponse response = UserInfoResponse.builder()
                .auditPass(auditPass)
                .auditPassMsg(auditPassMsg)
                .build();
        return ResultModel.success(response);
    }
}
